#!/bin/sh
# define source directory
MY_BIN_DIR=`dirname $_`
TAR_FILE=$1
TOP=`pwd`
BRANCH=gingerbread
BRANCH_DATE=20101222
SRC_DIR=android-${BRANCH}

set -e

if [ "x${TAR_FILE}" == "x" ]; then
    echo "ERROR: no tarfile provided"
    exit
fi

if [ ! -e ${TAR_FILE} ]; then
    echo "ERROR: tarfile does not exist"
    exit
fi

# get the android source
mkdir ${TOP}/${SRC_DIR}
cd ${TOP}/${SRC_DIR}
repo init -u https://android.googlesource.com/platform/manifest -b ${BRANCH} <<EOF
yes
EOF
# substitute manifest file
cd .repo
cp ${TOP}/${BRANCH}-${BRANCH_DATE}.xml manifests/
rm manifest.xml
ln -s manifests/gingerbread-20101222.xml manifest.xml
cd ..

repo sync

# pull down a usable kernel source.
git clone https://github.com/android/kernel_common.git -b archive/android-2.6.32 hardware/intel/linux-2.6

# untar the Intel source overtop of the Google Android source
tar -xvjf ../${TAR_FILE} --strip=1

# patch source
./patches/apply-patch.sh

